package com.sun.dailyprj.provider;


import android.os.Bundle;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

public class Dispatcher implements ILogDispatch{

    private final Map<String,ILogHandler> mHandlerMap;

    private Dispatcher(){
        mHandlerMap = new HashMap<>();
    }

    private static class DispatchHolder{
        private static Dispatcher instance = new Dispatcher();
    }

    public static Dispatcher getInstance(){
        return DispatchHolder.instance;
    }

    @Override
    public Bundle process(String method, String args, Bundle bundle) {
        ILogHandler iHandler = findHandler(method);
        if(iHandler != null){
            switch(args){
                case ILogHandler.Args.RECORD_ARGS:
                    return iHandler.record(method,args,bundle);
                case ILogHandler.Args.SEND_ARGS:
                    return iHandler.send(method,args,bundle);
                case ILogHandler.Args.DELETE_ARGS:
                    return iHandler.delete(method,args,bundle);
                default:
                    return null;
            }
        }
        return null;
    }

    @Override
    public void setHandler(String method, ILogHandler handler) {
        if(TextUtils.isEmpty(method)){
            throw new RuntimeException("method name is not allowed to null");
        }
        if(handler == null) {
            throw new RuntimeException("handler not allowed to null");
        }
        mHandlerMap.put(method,handler);
    }

    private ILogHandler findHandler(String method){
        return mHandlerMap.get(method);
    }
}
